package de.gwtprime.client.util;

import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SimpleHtmlSanitizer;

public class UserInputParser {

	public static String HIGHLIGHTER_OPEN_TAG = "<pre class='prettyprint'>";
	public static String HIGHLIGHTER_END_TAG = "</pre>";


	public static String escapeHTMLAndAddPrettifyTags(String input){
		if(input == null) {
			AppUtils.log("User input parser: Input should not be null");
			return "";
		}
		
		SafeHtml safeHtml = SimpleHtmlSanitizer.sanitizeHtml(input);
		
		// Replace <code> and </code> tag
		return safeHtml.asString().
			replaceAll("&lt;code&gt;", HIGHLIGHTER_OPEN_TAG).
			replaceAll("&lt;/code&gt;", HIGHLIGHTER_END_TAG);
		
	}
	
	/**
	 * Pretty print all code blocks on the page
	 */
	public static native void prettyPrint() /*-{
	  $wnd.prettyPrint();
	}-*/;
	

}
